

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" href="/img/favicon.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="Cheney">
  <meta name="keywords" content="Coding">
  
    <meta name="description" content="使用文件系统调用编写一个文件工具 filetools，使其具有以下功能：1.创建新文件 2.写文件 3.读文件 4.修改文件权限 5.查看当前文件权限 0.退出. 提示用户输入功能号，并根据用户输入的功能选择相应的功能。">
<meta property="og:type" content="article">
<meta property="og:title" content="Linux文件系统实验（C语言实现新建、打开、关闭、读写文件以及查改文件权限）">
<meta property="og:url" content="https://cheney822.gitee.io/2021/05/08/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F__Linux%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F%E5%AE%9E%E9%AA%8C%EF%BC%88C%E8%AF%AD%E8%A8%80%E5%AE%9E%E7%8E%B0%E6%96%B0%E5%BB%BA%E3%80%81%E6%89%93%E5%BC%80%E3%80%81%E5%85%B3%E9%97%AD%E3%80%81%E8%AF%BB%E5%86%99%E6%96%87%E4%BB%B6%E4%BB%A5%E5%8F%8A%E6%9F%A5%E6%94%B9%E6%96%87%E4%BB%B6%E6%9D%83%E9%99%90%EF%BC%89/index.html">
<meta property="og:site_name" content="Cheney blog">
<meta property="og:description" content="使用文件系统调用编写一个文件工具 filetools，使其具有以下功能：1.创建新文件 2.写文件 3.读文件 4.修改文件权限 5.查看当前文件权限 0.退出. 提示用户输入功能号，并根据用户输入的功能选择相应的功能。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://imgbed.cheney.cc/picgo/20210505172049268.png">
<meta property="article:published_time" content="2021-05-08T00:22:00.000Z">
<meta property="article:modified_time" content="2022-04-05T13:14:30.683Z">
<meta property="article:author" content="Cheney">
<meta property="article:tag" content="算法">
<meta property="article:tag" content="C语言">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://imgbed.cheney.cc/picgo/20210505172049268.png">
  
  
  <title>Linux文件系统实验（C语言实现新建、打开、关闭、读写文件以及查改文件权限） - Cheney blog</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4/github-markdown.min.css" />
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hint.css@2/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.css" />
  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"cheney822.gitee.io","root":"/","version":"1.8.14","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":1},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.4.1"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>Cheney Blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('https://imgbed.cheney.cc/Blog_config/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="Linux文件系统实验（C语言实现新建、打开、关闭、读写文件以及查改文件权限）">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2021-05-08 08:22" pubdate>
        2021年5月8日 早上
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      5.5k 字
    </span>
  

  
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      28 分钟
    </span>
  

  
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">Linux文件系统实验（C语言实现新建、打开、关闭、读写文件以及查改文件权限）</h1>
            
            <div class="markdown-body">
              <h2 id="题目描述"><a href="#题目描述" class="headerlink" title="题目描述:"></a>题目描述:</h2><p>使用文件系统调用编写一个文件工具 filetools，使其具有以下功能：</p>
<ul>
<li>1.创建新文件</li>
<li>2.写文件</li>
<li>3.读文件</li>
<li>4.修改文件权限</li>
<li>5.查看当前文件权限</li>
<li>0.退出</li>
<li>提示用户输入功能号，并根据用户输入的功能选择相应的功能。</li>
</ul>
<p>文件按可变记录文件组织，具体记录内容自行设计。</p>
<h2 id="算法设计"><a href="#算法设计" class="headerlink" title="算法设计:"></a>算法设计:</h2><h3 id="打开-新建文件-open"><a href="#打开-新建文件-open" class="headerlink" title="打开/新建文件(open)"></a>打开/新建文件(open)</h3><p>调用open可以打开或创建一个文件。</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><div class="code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></div></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&lt;sys/types.h&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&lt;sys/stat.h&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&lt;fcntl.h&gt;</span></span><br><span class="hljs-function"><span class="hljs-type">int</span> <span class="hljs-title">open</span><span class="hljs-params">(<span class="hljs-type">const</span> <span class="hljs-type">char</span> *pathname, <span class="hljs-type">int</span> oflag, ... <span class="hljs-comment">/* mode_t mode */</span>)</span></span>;<br></code></pre></td></tr></table></figure>
<p><strong>返回</strong>若成功为文件描述符，若出错为- 1<br><strong>参数：</strong><br>第一个参数<code>**pathname*</code>是要打开或创建的文件的名字。<br>第二个参数oflag参数可用来说明此函数的多个选择项。用下列一个或多个常数进行或运算构成<code>oflag*</code>参数(这些常数定义在&lt; fcntl . h &gt;头文件中, 在这三个常数中应当只指定一个)：</p>
<blockquote>
<p>O_RDONLY 只读打开<br>    O_WRONLY 只写打开<br>    O_RDWR 读、写打开</p>
</blockquote>
<p>第三个可选参数：</p>
<blockquote>
<p>O_APPEND 每次写时都加到文件的尾端。<br>    O_CREAT 若此文件不存在则创建它。使用此选择项时，需同时说明第三个参数mode，用其说明该新文件的存取许可权位。<br>    O_EXCL 如果同时指定了O_CREAT，而文件已经存在，则出错。这可测试一个文件是否存在。<br>    O_TRUNC 如果此文件存在，而且为只读或只写成功打开，则将其长度截短为0。<br>    O_NONBLOCK 如果pathname指的是一个FIFO、一个块特殊文件或一个字符特殊文件，则此选择项为此文件的本次打开操作和后续的I /<br>O操作设置非阻塞方式。<br>    O_SYNC 使每次write都等到物理I / O操作完成。</p>
</blockquote>
<h3 id="读文件-read"><a href="#读文件-read" class="headerlink" title="读文件(read)"></a>读文件(read)</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&lt;unistd.h&gt;</span></span><br><span class="hljs-function"><span class="hljs-type">ssize_t</span> <span class="hljs-title">read</span><span class="hljs-params">(<span class="hljs-type">int</span> fd, <span class="hljs-type">void</span> *buff, <span class="hljs-type">size_t</span> nbytes)</span> </span>;<br></code></pre></td></tr></table></figure>
<p>返回：读到的字节数，若已到文件尾为0，若出错为-1</p>
<h3 id="写文件-write"><a href="#写文件-write" class="headerlink" title="写文件(write)"></a>写文件(write)</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&lt;unistd.h&gt;</span></span><br><span class="hljs-function"><span class="hljs-type">ssize_t</span> <span class="hljs-title">write</span><span class="hljs-params">(<span class="hljs-type">int</span> fd, <span class="hljs-type">const</span> <span class="hljs-type">void</span> *buf, <span class="hljs-type">size_t</span> nbytes)</span> </span>;<br></code></pre></td></tr></table></figure>
<ul>
<li>返回：若成功为已写的字节数，若出错为-1</li>
<li>对于普通文件，写操作从文件的当前位移量处开始。如果在打开该文件时，指定了O_APPEND选择项，则在每次写操作之前，将文件位移量设置在文件的当前结尾处。在一次成功写之后，该文件位移量增加实际写的字节数。</li>
</ul>
<h3 id="查权限"><a href="#查权限" class="headerlink" title="查权限"></a>查权限</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-type">char</span>* pargv[<span class="hljs-number">4</span>]=&#123;<span class="hljs-string">&quot;ls&quot;</span>,<span class="hljs-string">&quot;-l&quot;</span>,<span class="hljs-string">&quot;file1&quot;</span>,<span class="hljs-literal">NULL</span>&#125;;<br> 显示文件权限 <span class="hljs-built_in">execv</span>(<span class="hljs-string">&quot;/bin/ls&quot;</span>,pargv); <br> <br></code></pre></td></tr></table></figure>
<ul>
<li>所以查询文件权限只需要：</li>
</ul>
<p>1.pargv[2]改为待操作的文件名<br>2.执行execv(“/bin/ls”,pargv); </p>
<p> 由于进程执行完execv后会退出而无法执行其他逻辑，于是fork一个子进程来作查权限的操作，主进程调用wait等子进程结束即可。</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-type">int</span> p =fork();<br>            <span class="hljs-keyword">if</span>(p==<span class="hljs-number">0</span>)<br>                <span class="hljs-built_in">check_file_mod</span>();<br>            <span class="hljs-keyword">else</span><br>                <span class="hljs-built_in">wait</span>(<span class="hljs-literal">NULL</span>);       <br></code></pre></td></tr></table></figure>

<h3 id="改权限chmod"><a href="#改权限chmod" class="headerlink" title="改权限chmod"></a>改权限chmod</h3><ul>
<li>linux下的文件权限分三级：owner，group，others三类。每类占权限位的三位。</li>
<li>r 表示可读取，w 表示可写入，x 表示可执行，X 表示只有当该档案是个子目录或者该档案已经被设定过为可执行。</li>
<li>其中：r=4，w=2，x=1<br>  例如：若要rwx属性则4+2+1=7；<div class="code-wrapper"><pre><code class="hljs">  若要rw-属性则4+2=6；
  若要r-x属性则4+1=7。 
</code></pre></div>
</li>
<li>常见权限：<blockquote>
<p>-rw——- (600) – 只有属主有读写权限。 </p>
</blockquote>
</li>
<li>rw-r–r– (644) – 只有属主有读写权限；而属组用户和其他用户只有读权限。 </li>
<li>rwx—— (700) – 只有属主有读、写、执行权限。 </li>
<li>rwxr-xr-x (755) – 属主有读、写、执行权限；而属组用户和其他用户只有读、执行权限。 </li>
<li>rwx–x–x (711) – 属主有读、写、执行权限；而属组用户和其他用户只有执行权限。 </li>
<li>rw-rw-rw- (666) – 所有用户都有文件读、写权限。这种做法不可取。 </li>
<li>rwxrwxrwx (777) – 所有用户都有读、写、执行权限。更不可取的做法。  </li>
</ul>
<h3 id="lseek调用"><a href="#lseek调用" class="headerlink" title="lseek调用"></a>lseek调用</h3><p>每个打开的文件都有一个与其相关联的“当前文件位移量”。它是一个非负整数，用以度量从文件开始处计算的字节数。通常，读、写操作都从当前文件位移量处开始，并使位移量增加所读或写的字节数。按系统默认，当打开一个文件时，除非指定O_APPEND选择项，否则该位移量被设置为0。</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&lt;sys/types.h&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&lt;unistd.h&gt;</span></span><br><span class="hljs-function"><span class="hljs-type">off_t</span> <span class="hljs-title">lseek</span><span class="hljs-params">(<span class="hljs-type">int</span> fd, <span class="hljs-type">off_t</span> offset, <span class="hljs-type">int</span> whence)</span> </span>;<br></code></pre></td></tr></table></figure>
<p>参数offset 的解释与参数whence的值有关</p>
<div class="code-wrapper"><pre><code class="hljs">若whence是SEEK_SET，则将该文件的位移量设置为距文件开始处offset 个字节

若whence是SEEK_CUR，则将该文件的位移量设置为其当前值加offset, offset可为正或负

若whence是SEEK_END，则将该文件的位移量设置为文件长度加offset, offset可为正或负
</code></pre></div>
<h2 id="具体代码"><a href="#具体代码" class="headerlink" title="具体代码:"></a>具体代码:</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br></pre></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-comment">/*</span><br><span class="hljs-comment">1.</span><br><span class="hljs-comment">open 	打开文件</span><br><span class="hljs-comment">creat 	创建新文件</span><br><span class="hljs-comment">close 	关闭文件描述字</span><br><span class="hljs-comment">read 	读文件</span><br><span class="hljs-comment">write 	写文件</span><br><span class="hljs-comment">*/</span><br><br><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&lt;sys/types.h&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&lt;sys/stat.h&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&lt;fcntl.h&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&lt;unistd.h&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&lt;stdio.h&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&lt;stdlib.h&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&lt;string.h&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&lt;sys/wait.h&gt;</span></span><br><br><span class="hljs-type">int</span> C_fd;           <span class="hljs-comment">//文件描述符</span><br><span class="hljs-type">char</span> filename[<span class="hljs-number">100</span>]; <span class="hljs-comment">//文件名</span><br><span class="hljs-type">int</span> isopen = <span class="hljs-number">0</span>;     <span class="hljs-comment">//打开状态</span><br><span class="hljs-type">char</span> *pargv[<span class="hljs-number">4</span>] = &#123;<span class="hljs-string">&quot;ls&quot;</span>, <span class="hljs-string">&quot;-l&quot;</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>&#125;;<br><br><span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">Open_Creat_file</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>&#123;<br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;请输入要打开/创建的文件名: &quot;</span>);<br>    <span class="hljs-built_in">scanf</span>(<span class="hljs-string">&quot;%s&quot;</span>, filename);<br>    C_fd = <span class="hljs-built_in">open</span>(filename, O_RDWR, <span class="hljs-number">0666</span>);<br>    <span class="hljs-keyword">if</span> (C_fd &gt;= <span class="hljs-number">0</span>)<br>    &#123;<br>        isopen = <span class="hljs-number">1</span>, <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%s已存在，打开成功\n&quot;</span>, filename);<br>        pargv[<span class="hljs-number">2</span>] = (<span class="hljs-type">char</span> *)<span class="hljs-built_in">malloc</span>(<span class="hljs-number">50</span>);<br>        <span class="hljs-built_in">strcpy</span>(pargv[<span class="hljs-number">2</span>], filename);<br>    &#125;<br>    <span class="hljs-keyword">else</span><br>    &#123;<br>        C_fd = <span class="hljs-built_in">open</span>(filename, O_CREAT, <span class="hljs-number">0666</span>);<br>        <span class="hljs-keyword">if</span> (C_fd &gt;= <span class="hljs-number">0</span>)<br>            isopen = <span class="hljs-number">1</span>, <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%s不存在，新建成功\n&quot;</span>, filename);<br>        <span class="hljs-keyword">else</span><br>            isopen = <span class="hljs-number">0</span>, <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;打开/新建%s失败\n&quot;</span>, filename);<br>    &#125;<br>    <span class="hljs-comment">//close(fd);</span><br>&#125;<br><span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">read_file</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>&#123;<br>    <span class="hljs-keyword">if</span> (!isopen)<br>    &#123;<br>        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;没有打开的文件，请先打开一个文件\n&quot;</span>);<br>        <span class="hljs-keyword">return</span>;<br>    &#125;<br>    <span class="hljs-keyword">else</span><br>        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;当前处理的文件是:%s\n&quot;</span>, filename);<br>    <span class="hljs-type">char</span> buffer[<span class="hljs-number">100</span>];<br><br>    <span class="hljs-built_in">lseek</span>(C_fd, <span class="hljs-number">0</span>, SEEK_SET); <span class="hljs-comment">//从头开始读</span><br>    <span class="hljs-type">int</span> t = <span class="hljs-built_in">read</span>(C_fd, buffer, <span class="hljs-number">1024</span>);<br>    <span class="hljs-keyword">if</span> (t != <span class="hljs-number">-1</span>)<br>        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;文件内容为:%s\n&quot;</span>, buffer);<br>    <span class="hljs-keyword">else</span><br>        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;读取失败\n&quot;</span>);<br>&#125;<br><span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">write_file</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>&#123;<br>    <span class="hljs-keyword">if</span> (!isopen)<br>    &#123;<br>        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;没有打开的文件，请先打开一个文件\n&quot;</span>);<br>        <span class="hljs-keyword">return</span>;<br>    &#125;<br>    <span class="hljs-keyword">else</span><br>        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;当前处理的文件是:%s\n&quot;</span>, filename);<br>    <span class="hljs-type">char</span> buffer[<span class="hljs-number">1024</span>];<br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;请输入待写入内容：\n&quot;</span>);<br>    <span class="hljs-built_in">scanf</span>(<span class="hljs-string">&quot;%s&quot;</span>, buffer);<br><br>    <span class="hljs-type">int</span> t = <span class="hljs-built_in">write</span>(C_fd, buffer, <span class="hljs-built_in">strlen</span>(buffer));<br>    <span class="hljs-keyword">if</span> (t == <span class="hljs-number">-1</span>)<br>        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;写入失败！\n&quot;</span>);<br>    <span class="hljs-keyword">else</span><br>        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;写入成功！\n&quot;</span>);<br>&#125;<br><span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">change_file_mod</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>&#123;<br>    <span class="hljs-keyword">if</span> (!isopen)<br>    &#123;<br>        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;没有打开的文件，请先打开一个文件\n&quot;</span>);<br>        <span class="hljs-keyword">return</span>;<br>    &#125;<br>    <span class="hljs-keyword">else</span><br>        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;当前处理的文件是:%s\n&quot;</span>, filename);<br>    <span class="hljs-type">int</span> new_mode;<br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;输入新的模式(十进制)&quot;</span>);<br>    <span class="hljs-built_in">scanf</span>(<span class="hljs-string">&quot;%d&quot;</span>, &amp;new_mode);<br>    <span class="hljs-type">int</span> mode_u = new_mode / <span class="hljs-number">100</span>;<br>    <span class="hljs-type">int</span> mode_g = new_mode / <span class="hljs-number">10</span> % <span class="hljs-number">10</span>;<br>    <span class="hljs-type">int</span> mode_o = new_mode % <span class="hljs-number">10</span>;<br>    new_mode = (mode_u * <span class="hljs-number">8</span> * <span class="hljs-number">8</span>) + (mode_g * <span class="hljs-number">8</span>) + mode_o; <span class="hljs-comment">//八进制转换</span><br>    <span class="hljs-type">int</span> t = <span class="hljs-built_in">chmod</span>(filename, new_mode);<br>    <span class="hljs-keyword">if</span> (t == <span class="hljs-number">-1</span>)<br>        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;改变模式失败！\n&quot;</span>);<br>    <span class="hljs-keyword">else</span>&#123;<br>        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;成功改变模式！\n&quot;</span>);<br>        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;新模式：%d\n&quot;</span>,new_mode);               <br>    &#125;<br>&#125;<br><span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">check_file_mod</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>&#123;<br>    <span class="hljs-keyword">if</span> (!isopen)<br>    &#123;<br>        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;没有打开的文件，请先打开一个文件\n&quot;</span>);<br>        <span class="hljs-keyword">return</span>;<br>    &#125;<br>    <span class="hljs-keyword">else</span><br>        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;当前处理的文件是:%s\n&quot;</span>, filename);<br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;文件权限为&quot;</span>);<br>    <span class="hljs-type">int</span> t = <span class="hljs-built_in">execv</span>(<span class="hljs-string">&quot;/bin/ls&quot;</span>, pargv);<br>    <span class="hljs-keyword">if</span> (t == <span class="hljs-number">-1</span>)<br>        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;查看失败\n&quot;</span>);<br>&#125;<br><span class="hljs-function"><span class="hljs-type">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>&#123;<br><br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;-----------------CC_File_Tools--------------\n&quot;</span>);<br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;1.打开(不存在则新建)       2.写文件       3.读文件  \n&quot;</span>);<br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;4.改权限     5.查权限       0.退出    \n&quot;</span>);<br>    <span class="hljs-comment">//printf(&quot;------------------------------------\n&quot;);</span><br><br>    <span class="hljs-type">int</span> choice = <span class="hljs-number">-1</span>;<br>    <span class="hljs-keyword">while</span> (<span class="hljs-number">1</span>)<br>    &#123;<br>        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;-------------请输入选择：-----------\n&quot;</span>);<br>        <span class="hljs-built_in">scanf</span>(<span class="hljs-string">&quot;%d&quot;</span>, &amp;choice);<br>        <span class="hljs-keyword">switch</span> (choice)<br>        &#123;<br>        <span class="hljs-keyword">case</span> <span class="hljs-number">0</span>:<br>        &#123;<br>            <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;\n\t感谢使用！\n\n&quot;</span>);<br>            <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>        &#125;<br>        <span class="hljs-keyword">case</span> <span class="hljs-number">1</span>:<br>        &#123;<br>            <span class="hljs-built_in">Open_Creat_file</span>();<br>            <span class="hljs-keyword">break</span>;<br>        &#125;<br>        <span class="hljs-keyword">case</span> <span class="hljs-number">2</span>:<br>        &#123;<br>            <span class="hljs-built_in">write_file</span>();<br>            <span class="hljs-keyword">break</span>;<br>        &#125;<br>        <span class="hljs-keyword">case</span> <span class="hljs-number">3</span>:<br>        &#123;<br>            <span class="hljs-built_in">read_file</span>();<br>            <span class="hljs-keyword">break</span>;<br>        &#125;<br>        <span class="hljs-keyword">case</span> <span class="hljs-number">4</span>:<br>        &#123;<br>            <span class="hljs-built_in">change_file_mod</span>();<br>            <span class="hljs-keyword">break</span>;<br>        &#125;<br>        <span class="hljs-keyword">case</span> <span class="hljs-number">5</span>:<br>        &#123;<br>            <span class="hljs-type">int</span> p =fork();<br>            <span class="hljs-keyword">if</span>(p==<span class="hljs-number">0</span>)<br>                <span class="hljs-built_in">check_file_mod</span>();<br>            <span class="hljs-keyword">else</span><br>                <span class="hljs-built_in">wait</span>(<span class="hljs-literal">NULL</span>);            <br>            <span class="hljs-keyword">break</span>;<br>        &#125;<br>        <span class="hljs-keyword">default</span>:<br>        &#123;<br>            <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;您的输入有误，请重新输入！！！\n&quot;</span>);<br>        &#125;<br>        &#125;<br>    &#125;<br><br>    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<h2 id="运行结果"><a href="#运行结果" class="headerlink" title="运行结果:"></a>运行结果:</h2><p><img src="https://imgbed.cheney.cc/picgo/20210505172049268.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/">操作系统</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/%E7%AE%97%E6%B3%95/">算法</a>
                    
                      <a class="hover-with-bg" href="/tags/C%E8%AF%AD%E8%A8%80/">C语言</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2021/05/09/%E7%BC%96%E8%AF%91%E5%8E%9F%E7%90%86__%E7%BC%96%E8%AF%91%E5%8E%9F%E7%90%86%E5%A4%8D%E4%B9%A0%E6%80%BB%E7%BB%93%EF%BC%88%E7%B2%BE%E5%8D%8E%E7%89%88%EF%BC%89/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">编译原理复习总结（精华版）</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2021/05/07/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F__Linux%E4%B8%8B%E5%A4%9A%E8%BF%9B%E7%A8%8B%E9%80%9A%E4%BF%A1%EF%BC%88signal%EF%BC%8Cpipe%EF%BC%89/">
                        <span class="hidden-mobile">Linux下多进程通信（signal，pipe）</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://cheney822.gitee.io/" target="_blank" rel="nofollow noopener"><span>备用网址</span></a> 
  </div>
  

  
  <!-- 备案信息 -->
  <div class="beian">
    <span>
      <a href="http://beian.miit.gov.cn/" target="_blank" rel="nofollow noopener">
        皖ICP备2022002876号-1
      </a>
    </span>
    
  </div>


  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  <script  src="/js/local-search.js" ></script>



  
    <script  src="/js/img-lazyload.js" ></script>
  



  



  
    <script  src="https://cdn.jsdelivr.net/npm/tocbot@4/dist/tocbot.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4/anchor.min.js" ></script>
  
  
    <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js" ></script>
  






  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
        typing(title);
      
    })(window, document);
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>


</body>
</html>
